﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EHSubmit.Model;
using EHSubmit.Util;

namespace EHSubmit.Logic
{
    /// <summary>
    ///AnnouncesManager 的摘要说明
    /// </summary>
    public class AnnouncesManager
    {
        private const string SQL_INSERT_Announces = "INSERT INTO Announces(Topic,Body,UserID,UserName,SubmitTime,DepartmentID,DepartmentName,rescnt,resLastTime) values (@Topic,@Body,@UserID,@UserName,@SubmitTime,@DepartmentID,@DepartmentName,@rescnt,@resLastTime)";
        private const string PARM_AID = "@AID";
        private const string PARM_Topic = "@Topic";
        private const string PARM_Body = "@Body";
        private const string PARM_UserID = "@UserID";
        private const string PARM_UserName = "@UserName";
        private const string PARM_SubmitTime = "@SubmitTime";
        private const string PARM_DepartmentID = "@DepartmentID";
        private const string PARM_DepartmentName = "@DepartmentName";
        private const string PARM_rescnt = "@rescnt";
        private const string PARM_resLastTime = "@resLastTime";

        public AnnouncesManager() {}


        /// <summary>
        /// Announces信息被添加
        /// </summary>
        /// <param name="Announces">帖子对象</param>
        public void AddAnnounces(Announces announces)
        {
            try
            {
                int i;
                SqlParameter[] parms = new SqlParameter[9];
                parms[0] = new SqlParameter(PARM_Topic, SqlDbType.NVarChar, 50);
                parms[1] = new SqlParameter(PARM_Body, SqlDbType.NVarChar, 400);
                parms[2] = new SqlParameter(PARM_UserID, SqlDbType.NVarChar, 13);
                parms[3] = new SqlParameter(PARM_UserName, SqlDbType.NVarChar, 5);
                parms[4] = new SqlParameter(PARM_SubmitTime,SqlDbType.DateTime);
                parms[5] = new SqlParameter(PARM_DepartmentID, SqlDbType.Int);
                parms[6] = new SqlParameter(PARM_DepartmentName, SqlDbType.NVarChar, 20);
                parms[7] = new SqlParameter(PARM_rescnt, SqlDbType.Int);
                parms[8] = new SqlParameter(PARM_resLastTime, SqlDbType.DateTime);
                parms[0].Value = announces.Topic;
                parms[1].Value = announces.Body;
                parms[2].Value = announces.UserID;
                parms[3].Value = announces.UserName;
                parms[4].Value = announces.SubmitTime;
                parms[5].Value = announces.DepartmentID;
                parms[6].Value = announces.DepartmentName;
                parms[7].Value = announces.rescnt;
                parms[8].Value = announces.resLastTime;

                i = (int)SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, SQL_INSERT_Announces, parms);

            }
            catch
            { 
                //处理
            }
        }


        /// <summary>
        /// 获取单页的消息集合    默认记录数为10  
        /// </summary>
        /// <param name="pageIndex">当前页的页码</param>
        /// <returns>帖子消息集合</returns>
        public IList<Announces> GetAnnounces(int pageIndex)
        {
            PagedDataProvider pagedDataProvider = new PagedDataProvider();
            pagedDataProvider.TbName = "Announces";
            pagedDataProvider.FieldsToReturn = "*";
            pagedDataProvider.PageSize = 15;                //默认记录数为10
            pagedDataProvider.PageIndex = pageIndex;
            pagedDataProvider.DoCount = false;
            pagedDataProvider.IsDesc = true;
            pagedDataProvider.FieldToOrder = "resLastTime";
            
            SqlDataReader rdrAnnounces = pagedDataProvider.GetResults();
            IList<Announces> AnnoucesList = new List<Announces>();
            try
            {
                while (rdrAnnounces.Read())
                {
                    Announces announce = new Announces();

                    announce.AID = (Int32)rdrAnnounces["AID"];
                    announce.Topic = rdrAnnounces["Topic"] is DBNull ? string.Empty : (string)rdrAnnounces["Topic"];
                    announce.Body = rdrAnnounces["Body"] is DBNull ? string.Empty : (string)rdrAnnounces["Body"];
                    announce.UserID = rdrAnnounces["UserID"] is DBNull ? string.Empty : (string)rdrAnnounces["UserID"];
                    announce.UserName = rdrAnnounces["UserName"] is DBNull ? string.Empty : (string)rdrAnnounces["UserName"];
                    announce.DepartmentID = (Int32)rdrAnnounces["DepartmentID"];
                    announce.DepartmentName = rdrAnnounces["DepartmentName"] is DBNull ? string.Empty : (string)rdrAnnounces["DepartmentName"];
                    announce.SubmitTime = rdrAnnounces["SubmitTime"] is DBNull ? DateTime.Now : (DateTime)rdrAnnounces["SubmitTime"];
                    announce.resLastTime = rdrAnnounces["resLastTime"] is DBNull ? DateTime.Now : (DateTime)rdrAnnounces["resLastTime"];
                    announce.rescnt = (Int32)rdrAnnounces["rescnt"];


                    AnnoucesList.Add(announce);
                }
            }
            finally
            {
                if (rdrAnnounces != null)   //关闭与数据库连接
                    rdrAnnounces.Close();
            }
            return AnnoucesList;
        }


        /// <summary>
        /// 获取帖子总数量
        /// </summary>
        /// <returns>帖子数量</returns>
        public int GetAnnouncesPageCount()
        {
            PagedDataProvider pagedDataProvider = new PagedDataProvider();
            pagedDataProvider.TbName = "Announces";
            pagedDataProvider.DoCount = true;
           
            pagedDataProvider.TotalCount = pagedDataProvider.GetRecordCount();
            if (pagedDataProvider.TotalCount == -1)
            {
                //有待处理解决
                return -1;
            }
            else
            {
                return pagedDataProvider.TotalCount;
            }
        }


    }
}
